package com.baidu.lenovo.autotest;

import android.Manifest;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.baidu.aip.asrwakeup3.core.mini.ActivityMiniRecog;

import java.util.ArrayList;
import android.view.View.OnClickListener;


public class MainActivity extends AppCompatActivity implements OnClickListener,ServiceConnection {
    private MyService.Binder binder = null;
    private EditText stampTime;
    private EditText repeatNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.asrButton).setOnClickListener(this);
        findViewById(R.id.playButton).setOnClickListener(this);
        findViewById(R.id.textButton).setOnClickListener(this);
        findViewById(R.id.syncButton).setOnClickListener(this);
        findViewById(R.id.ttsButton).setOnClickListener(this);
        stampTime = (EditText) findViewById(R.id.edit1);
        repeatNum = (EditText) findViewById(R.id.edit2);
//        initButtons();
        initPermission();
    }

//    private void initButtons() {
//        Button b1 = (Button) findViewById(R.id.synthButton);
//        Button b2 = (Button) findViewById(R.id.miniButton);
//        Button b3 = (Button) findViewById(R.id.saveTtsFileButton);
//
//        b1.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                startAct(TTSActivity.class);
////                startAct(SynthActivity.class);
//            }
//        }); // 离在线语音合成
//        b2.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//
//            }
//        }); // 语音识别
//        b3.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
////                startAct(SaveFileActivity.class);
//            }
//        });
//    }

    /**
     * android 6.0 以上需要动态申请权限
     */
    private void initPermission() {
        String[] permissions = {
                Manifest.permission.INTERNET,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.MODIFY_AUDIO_SETTINGS,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_SETTINGS,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.ACCESS_WIFI_STATE,
                Manifest.permission.CHANGE_WIFI_STATE
        };

        ArrayList<String> toApplyList = new ArrayList<String>();

        for (String perm : permissions) {
            if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this, perm)) {
                toApplyList.add(perm);
                // 进入到这里代表没有权限.
            }
        }
        String[] tmpList = new String[toApplyList.size()];
        if (!toApplyList.isEmpty()) {
            ActivityCompat.requestPermissions(this, toApplyList.toArray(tmpList), 123);
        }

    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        // 此处为android 6.0以上动态授权的回调，用户自行实现。
    }


    private void startAct(Class activityClass) {
        startActivity(new Intent(this, activityClass));
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        binder = (MyService.Binder) service;
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.asrButton://点击绑定服务按钮
                startAct(ASRActivity.class);
            break;
            case R.id.ttsButton://点击绑定服务按钮
                startAct(TTSActivity.class);
                break;
            case R.id.textButton://点击绑定服务按钮
                startAct(TextCompareActivity.class);
                break;
            case R.id.playButton://点击绑定服务按钮
                bindService(new Intent(this, MyService.class), this, Context.BIND_AUTO_CREATE);
                break;
            case R.id.syncButton://同步数据按钮
                if (binder != null){
                int[] arr=new int[2];
                arr[0]=Integer.parseInt(stampTime.getText().toString());
                arr[1]=Integer.parseInt(repeatNum.getText().toString());
                    binder.setData(arr);
                }
                break;
        }
    }
}